//
//  YXFlutterRouter.m
//  flutter_yuanxin_Example
//
//  Created by GMS on 2020/10/19.
//  Copyright © 2020 Arthur. All rights reserved.
//

#import "YXFlutterRouter.h"
#import <UIKit/UIKit.h>

@interface YXFlutterRouter ()

@end

@implementation YXFlutterRouter

-(void)open:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion
{
    if ([url containsString:@"flutter://nativePage"]) {
        
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:urlParams];
        [dic setObject:@"push" forKey:@"type"];
        BOOL animated = [exts[@"animated"] boolValue];
        UIViewController *vc = exts[@"vc"];
        FLBFlutterViewContainer * containerVC = [[FLBFlutterViewContainer alloc] init];
        self.navigationController = vc.navigationController;
        [containerVC setName:url params:dic];
        [vc.navigationController pushViewController:containerVC animated:animated];
        if (completion) {
            completion(YES);
        }
    }
}
-(void)present:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion
{
    BOOL animated = [exts[@"animated"] boolValue];
    UIViewController *vc = exts[@"vc"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:urlParams];
    [dic setObject:@"present" forKey:@"type"];
    FLBFlutterViewContainer * containerVC = [[FLBFlutterViewContainer alloc] init];
    self.containerVC = containerVC;
    [containerVC setName:url params:dic];
    [vc presentViewController:containerVC animated:animated completion:^{
        if (completion) {
            completion(YES);
        }
    }];
}
-(void)close:(NSString *)uid result:(NSDictionary *)result exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion
{
    if ([uid containsString:@"flutter://closePage"]) {
        NSLog(@"关闭页面时,传递的信息%@",result);
        
        NSDictionary *parasm = result[@"params"];
        NSString *type = parasm[@"type"];
        
        if ([type isEqualToString:@"push"]) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self.containerVC dismissViewControllerAnimated:YES completion:nil];
        }
    }else{
        BOOL animated = [exts[@"animated"] boolValue];
        FLBFlutterViewContainer *vc = (id)self.navigationController.presentedViewController;
        if([vc isKindOfClass:FLBFlutterViewContainer.class] && [vc.uniqueIDString isEqual: uid]){
           [vc dismissViewControllerAnimated:animated completion:^{}];
        }else{
           [self.navigationController popViewControllerAnimated:animated];
        }
    }
}


@end
